package Dao.Role.Imp;

import Dao.Role.ReplyGroupDao;
import Entity.ReplyGroup;
import Util.DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReplyGroupDaoImp implements ReplyGroupDao {
    private PreparedStatement ps = null;
    private ResultSet rs = null;


    @Override
    public void insertGroup(ReplyGroup replyGroup, Connection conn) {
        String sql = " insert into reply_group (group_no,group_leader,group_member)" +
                " values (?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, replyGroup.getGroupNO());
            ps.setString(2, replyGroup.getGroupLeader());
            ps.setString(3, replyGroup.getGroupMember());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
    }

    @Override
    public void delGroup(Connection conn) {

        String sql = " delete  from reply_group ";
        try {
            ps = conn.prepareStatement(sql);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
    }

    @Override
    public List<ReplyGroup> getAllGroup(Connection conn) {
        List<ReplyGroup> replyGroups = new ArrayList<>();
        String sql = " select id,group_no,group_leader ,group_member ,group_time,group_address,stu_sum from reply_group ";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer id = rs.getInt(1); //主键
                String groupNO = rs.getString(2); //答辩组成员
                String groupLeader = rs.getString(3); //答辩组长
                String groupMember = rs.getString(4); //答辩组成员
                String groupTime = rs.getString(5); //答辩时间
                String groupAddress = rs.getString(6);  // 答辩地址
                Integer stuSum = rs.getInt(7); //已有答辩学生人数
                ReplyGroup replyGroup = new ReplyGroup(id, groupNO, groupLeader, groupMember, groupTime, groupAddress, stuSum);
                replyGroups.add(replyGroup);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return replyGroups;
    }

    @Override
    public Integer addTimeAndAddress(Integer id, String replyTime, String replyAddress, Connection conn) {
        int res = 0;
        String sql = " update reply_group set group_time = ?,group_address = ?" +
                " where id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, replyTime);
            ps.setString(2, replyAddress);
            ps.setInt(3, id);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }

    @Override
    public Integer updateStuSumById(Integer id, Connection conn) {
        int res = 0;
        String sql = " update reply_group set stu_sum = stu_sum + 1 " +
                " where id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }

    @Override
    public void setNOStu(Connection conn) {
        String sql = " update reply_group set stu_sum = 0 ";
        try {
            ps = conn.prepareStatement(sql);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
    }


    /**
     * 根据学号查询答辩组信息
     */
    @Override
    public ReplyGroup getStuDefence(String sno, Connection conn) {

        ReplyGroup replyGroup = new ReplyGroup();
        try {
            String sql = " select id, group_no, group_leader, group_member, group_time, group_address, stu_sum\n" +
                    " from reply_group \n" +
                    " where group_no =(SELECT group_no from paper where sno = ?)";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, sno);
            rs = ps.executeQuery();
            if (rs.next()) {
                replyGroup.setId(rs.getInt("id"));
                replyGroup.setGroupNO(rs.getString("group_no"));
                replyGroup.setGroupLeader(rs.getString("group_leader"));
                replyGroup.setGroupMember(rs.getString("group_member"));
                replyGroup.setGroupTime(rs.getString("group_time"));
                replyGroup.setGroupAddress(rs.getString("group_address"));
                replyGroup.setStuSum(rs.getInt("stu_sum"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return replyGroup;
    }
}
